package com.service.infoEntry.service.impl;

import com.service.config.utils.ModelMapperUtil;
import com.service.infoEntry.dao.RoleDao;
import com.service.infoEntry.entity.Permission;
import com.service.infoEntry.entity.Role;
import com.service.infoEntry.service.RoleService;
import com.service.infoEntry.vo.RoleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;

    /**
     * 查询用户在团队中的角色
     * @param teamId
     * @param userId
     * @return
     */
    @Override
    public RoleVo queryRoleInTeam(String teamId, String userId) {
        List<Permission> permissionList;
        RoleVo roleVo = null;
        Role role = roleDao.queryRoleInTeam(teamId, userId);
        if(null != role)
        {
            roleVo = ModelMapperUtil.strictMap(role, RoleVo.class);
            roleVo.setRoleId(role.getId());
            permissionList = roleDao.queryPermissionsByRoleId(role.getId());
            if(null != permissionList)
            {
                roleVo.setPermissions(permissionList);
            }
        }
        return roleVo;
    }

    @Override
    public RoleVo queryRoleInTeamWithOutPermissions(String teamId, String userId) {
        Role role = roleDao.queryRoleInTeam(teamId, userId);
        RoleVo roleVo = null;
        if(null != role)
        {
            roleVo = ModelMapperUtil.strictMap(role, RoleVo.class);
            roleVo.setRoleId(role.getId());
        }
        return roleVo;
    }
}
